package com.codedancer.campus.client.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.codedancer.campus.client.service.ISystemMessageService;
import com.codedancer.campus.client.service.bo.systemMessage.SystemMessagePageSearchBo;
import com.codedancer.campus.client.service.vo.systemMessage.SystemMessageVo;
import com.codedancer.campus.common.exception.BusinessException;
import com.codedancer.campus.mapper.SystemMessageMapper;
import com.codedancer.campus.po.SystemMessage;
import com.codedancer.campus.qo.systemMessage.SystemMessageSearchQo;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class SystemMessageServiceImpl implements ISystemMessageService {

    @Autowired
    private SystemMessageMapper systemMessageMapper;

    /**
     * 分页查询系统消息
     *
     * @param systemMessagePageSearchBo
     * @return
     */
    @Override
    public IPage<SystemMessageVo> page(SystemMessagePageSearchBo systemMessagePageSearchBo) {
        SystemMessageSearchQo systemMessageSearchQo = BeanUtil
            .copyProperties(systemMessagePageSearchBo, SystemMessageSearchQo.class);
        IPage<SystemMessage> systemMessageIPage = systemMessageMapper.pageToClient(
            new Page<SystemMessage>(systemMessagePageSearchBo.getPage(),
                systemMessagePageSearchBo.getSize()), systemMessageSearchQo);
        IPage<SystemMessageVo> systemMessageVoIPage = new Page<>();
        List<SystemMessageVo> systemMessageVos;
        systemMessageVos = systemMessageIPage.getRecords().stream().map(systemMessage -> BeanUtil
            .copyProperties(systemMessage, SystemMessageVo.class)).collect(Collectors.toList());
        systemMessageVoIPage.setTotal(systemMessageIPage.getTotal());
        systemMessageVoIPage.setRecords(systemMessageVos);
        return systemMessageVoIPage;
    }

    /**
     * 单个消息已读
     *
     * @param uuid
     */
    @Override
    public void singleRead(String uuid) {
        SystemMessage systemMessage = systemMessageMapper.findById(uuid)
            .orElseThrow(() -> new BusinessException("系统消息不存在"));
        if (systemMessage.getIsRead()) {
            throw new BusinessException("该消息已读");
        }
        systemMessage.setIsRead(true);
        systemMessageMapper.updateById(systemMessage);
    }

    /**
     * 消息全部已读
     */
    @Override
    public void allRead(String currentUserUuid) {
        systemMessageMapper.allRead(currentUserUuid);
    }

    /**
     * 是否存在未读系统消息
     *
     * @param currentLoginUuid
     * @return
     */
    @Override
    public Boolean isExistsNoRead(String currentLoginUuid) {
        return systemMessageMapper.isExistsNoRead(currentLoginUuid);
    }

    /**
     * 删除系统消息
     *
     * @param uuid
     */
    @Override
    public void delete(String uuid) {
        systemMessageMapper.deleteById(uuid);
    }
}
